﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using UWBGL_WinForms_Lib1;
using UWBGL_XNA_Lib10;

namespace XNA_WCSupport
{
	class DrawAndMouseHandler : UWB_XNAWindowHandler
	{
		private UWBGL_UIWindow mWindow;
		private int hwX, hwY, dvX, dvY;

		private int lastMouseX; // to + or - small view's size
		private bool _bZooming;
		int deltaX;

		public DrawAndMouseHandler()
		{
			mWindow = new UWBGL_UIWindow(true);
		}

		public bool Initialize(Control PlaceHolder)
		{
			hwX = hwY = dvX = dvY = 0;
			lastMouseX = 0;
			_bZooming = false;

			if (!mWindow.ReplaceFormControl(PlaceHolder.Handle))
				return false;

			if (!mWindow.AttachHandler(this))
				return false;

			return true;
		}

		public override void DrawGraphics()
		{
			//float height = 0;
			//float width = 0;
			//Program.getModel().getWorldSize(ref width, ref height);
			//width = height * getAspectRatio();

			//Matrix World2NDC = new Matrix();
			//Vector3 scale = new Vector3(2f / width, 2f / height, 1f);
			//Vector3 translate = new Vector3(-1f, -1f, 0f);
			//World2NDC = Matrix.CreateScale(scale) * Matrix.CreateTranslation(translate);

			//UWB_XNAGraphicsSystem.m_TheAPI.setViewMatrix(World2NDC);

			//BeginScene();

			//Program.getModel().drawModel();

			//EndSceneAndShow();

			BeginScene();

			loadW2NDCXform();

			Program.getModel().drawModel();

			EndSceneAndShow();
		}

		public override void OnMouseButton(bool down, MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			string key = "";
			Keys modkey = Control.ModifierKeys;
			if (modkey == Keys.Control)
				key += "[CTRL]";
			if (modkey == Keys.Shift)
				key += "[SHIFT]";
			if (key.Length == 0)
				key += "[NONE]";

			string btn = "";
			switch (e.Button)
			{
				case MouseButtons.Left:
					btn = "LMB";
					if (down)
					{
						Program.getModel().computeWorldCoordinates(mWindow.Width, mWindow.Height, deviceX, deviceY); // world coordinate
						Program.getModel().moveRedbox(Program.getModel().worldX, Program.getModel().worldY);
					}
					else
					{
						
					}
					break;
				case MouseButtons.Right:
					btn = "RMB";
					if (down)
					{
						_bZooming = true;
						lastMouseX = deviceX;
						Program.getModel().setupZoom();
					}
					else
						_bZooming = false;
					break;
				case MouseButtons.Middle:
					btn = "MMB";
					break;
			}
			string msg = "Viewport: " + btn + ": Position: (" + deviceX.ToString() + " " + deviceY.ToString() + ") Keys: " + key;
			//UWB_Utility.echoToStatusArea(msg);
		}

		public override void OnMouseMove(MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			
			if (_bZooming)
			{
				deltaX = deviceX - lastMouseX;
				float zoomFactor = 1;// init

				if (deltaX > 50) // 2x limit
					zoomFactor = 2;
				if (deltaX < -45) // .1 limit
					zoomFactor = 0.1f;

				if (zoomFactor == 1) // limits not past
				{
					//int deltaX = deviceX - lastMouseX;
					if (deltaX > 0) // zoom in
						zoomFactor = (((float)deltaX) / 50f) + 1f;
					if (deltaX < 0) // zoom out
						zoomFactor = 1f - ((-(float)deltaX) / 45f) * 0.9f;
					if (deltaX == 0)
						zoomFactor = 1;
				}
			
				Program.getModel().setZoom(zoomFactor);
			}

			string key = "";
			Keys modkey = Control.ModifierKeys;
			if (modkey == Keys.Control)
				key += "[CTRL]";
			if (modkey == Keys.Shift)
				key += "[SHIFT]";
			if (key.Length == 0)
				key += "[NONE]";

			string btn = "";
			switch (e.Button)
			{
				case MouseButtons.Left:
					btn = "LMB";
					Program.getModel().computeWorldCoordinates(mWindow.Width, mWindow.Height, deviceX, deviceY); // world coordinate
					Program.getModel().moveRedbox(Program.getModel().worldX, Program.getModel().worldY);
					break;
				case MouseButtons.Right:
					btn = "RMB";

					break;
				case MouseButtons.Middle:
					btn = "MMB";
					break;
			}

			string msg = "Big window: " + btn + ": Position: (" + deviceX.ToString() + " " + deviceY.ToString() + ") Keys: " + key;
			////UWB_Utility.echoToStatusArea(msg);

			Program.getModel().HoverLocation = "Big Window";
			Program.getModel().hardwareX = e.X;
			Program.getModel().hardwareY = e.Y;
			Program.getModel().deviceX = deviceX;
			Program.getModel().deviceY = deviceY;
			Program.getModel().computeWorldCoordinates(mWindow.Width, mWindow.Height, deviceX, deviceY); // world coordinate
		}

		public int Width
		{
			get { return mWindow.Width; }
			set { mWindow.Width = value; }
		}
		public int Height
		{
			get { return mWindow.Height; }
			set { mWindow.Height = value; }
		}

		#region get/set for coordinates
		public int hardwareX
		{
			get
			{
				return hwX;
			}

			set
			{
				hwX = value;
			}
		}

		public int hardwareY
		{
			get
			{
				return hwY;
			}

			set
			{
				hwY = value;
			}
		}

		public int deviceX
		{
			get
			{
				return dvX;
			}

			set
			{
				dvX = value;
			}
		}

		public int deviceY
		{
			get
			{
				return dvY;
			}

			set
			{
				dvY = value;
			}
		}

		#endregion
	}
}
